package com.mgr.dao.impl;

import java.util.List;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.mgr.dao.AccountDao;
import com.mgr.model.Accounts;
import com.mgr.model.Customers;

@Repository
public class AccountDaoImpl implements AccountDao{
	
	@Autowired
	private SessionFactory session;

	@Override
	public Accounts getAccount(int accId) {
		return (Accounts)session.getCurrentSession().get(Accounts.class, accId);
	}

	@Override
	public List<Accounts> getAccountByCustId(int cust_Id) {
		
		String hql = "FROM Accounts A WHERE A.custId = :custId";
		Query query = session.getCurrentSession().createQuery(hql);
		query.setParameter("custId",cust_Id);
		List results = query.list();
		return results;
	}
	
	@Override
	public Customers getCustomerByAccId(int acc_Id) {
		String hql = "SELECT C FROM Customers C, Accounts A WHERE A.accId = :accId AND A.custId = C.custId";
		Query query = session.getCurrentSession().createQuery(hql);
		query.setParameter("accId",acc_Id);
		return (Customers) query.uniqueResult();
	}
	@Override
	public void updateAccount(Accounts account) {
		session.getCurrentSession().update(account);
	}

}
